package org.dtrd.modules.flup.controller;


import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.dtrd.base.BaseController;
import org.dtrd.common.api.vo.Result;
import org.dtrd.common.aspect.annotation.AutoLog;
import org.dtrd.modules.base.service.BaseCommonService;
import org.dtrd.modules.flup.entity.bean.FlupTemplateProjectDetail;
import org.dtrd.modules.flup.entity.bean.FlupTemplateProjectListInfo;
import org.dtrd.modules.flup.entity.request.FlupTemplateProjectListRequest;
import org.dtrd.modules.flup.service.IDtrdEntRdFlupAppointmentRecordService;
import org.dtrd.modules.flup.service.IDtrdEntRdFlupTemplateProjectService;
import org.dtrd.modules.flup.service.IDtrdRlFlupTemplateProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static org.dtrd.common.constant.CommonConstant.*;
import static org.dtrd.config.system.SystemState.ResponseState.*;

/**
 * <p>
 * 复诊模版项目 前端控制器
 * </p>
 * @author QR
 * @since 2021-12-21
 */
@Api(tags = "复诊随访记录")
@RestController
@RequestMapping("/flup/template/project/")
public class FlupTemplateProjectController extends BaseController {

    @Autowired
    private IDtrdEntRdFlupTemplateProjectService flupTemplateProjectService;
    @Autowired
    private IDtrdRlFlupTemplateProjectService flupTemplateProjectRlService;
    @Autowired
    private BaseCommonService baseCommonService;
    @Autowired
    private IDtrdEntRdFlupAppointmentRecordService appointmentRecordService;

    @ApiOperation(value = "查询复诊模版项目列表", response = FlupTemplateProjectListInfo.class)
    @PostMapping("list")
    @AutoLog(value="查询复诊模版项目列表", operateType = OPERATE_TYPE_QUERY)
    public Result<?> getFlupTemplateProjectList(@RequestBody FlupTemplateProjectListRequest request){
        List<FlupTemplateProjectListInfo> list = flupTemplateProjectService.getFlupTemplateProjectList(request);
        String logContent = String.format("查询复诊模版项目列表, 查询复诊模版id: %d", request.getTemplateId());
        baseCommonService.addLog(logContent, OPERATE_TYPE_QUERY);
        return Result.OK(list);
    }

    @ApiOperation("新增或修改复诊模版项目")
    @PostMapping("edit")
    public Result<?> saveOrUpdateFlupTemplateProject(@RequestBody FlupTemplateProjectDetail dto){
        // 校验参数：模版项目名字
        if (StrUtil.isBlank(dto.getTemplateProjectName())) {
            return error(STATE_PARAMS_MISSING);
        }
        // 排序不给为负数
        if (dto.getOrderNum() < 0) {
            return error(STATE_PARAMS_ERROR);
        }
        Integer projectId = dto.getDataId();
        // 新增时不能有相同的复诊模版项目名字
        if (flupTemplateProjectService.isInValidTemplateProjectName(dto.getTemplateProjectName(), projectId, dto.getOrgCode())) {
            return Result.error("不能有相同名字的复诊模版项目");
        }
        // 判断是否为固定的复诊模版项目
        if (projectId != null && flupTemplateProjectService.isFixedFlupTemplateProject(projectId)) {
            return Result.error("不可修改固定项目内容");
        }
        boolean result = flupTemplateProjectService.saveOrUpdateFlupTemplateProject(dto);
        String logContent = String.format("新增或修改复诊模版项目, 修改模版项目id: %d", projectId);
        baseCommonService.addLog(logContent, OPERATE_TYPE_SAVE_OR_UPDATE);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }

    @ApiOperation("删除复诊模版项目")
    @GetMapping("remove")
    public Result<?> removeFlupTemplateProject(@RequestParam Integer id){
        if(isIntParamInvalid(id)) {
            return error(STATE_PARAMS_MISSING);
        }
        // 判断是否为固定的复诊模版项目
        if (flupTemplateProjectService.isFixedFlupTemplateProject(id)) {
            return Result.error("不可删除固定项目");
        }
        if (flupTemplateProjectRlService.hasLinkTemplate(id)) {
            return Result.error("该复诊模版项目已关联复诊模版");
        }
        if (appointmentRecordService.hasProjectLinkPatient(id)){
            return Result.error("该复诊模版项目已有患者预约");
        }
        boolean result = flupTemplateProjectService.removeFlupTemplateProject(id);
        String logContent = String.format("删除复诊模版项目, 删除id: %d", id);
        baseCommonService.addLog(logContent, OPERATE_TYPE_DELETE);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }
}
